package com.smart.controller;

import java.util.Map;
import java.util.Set;
import java.util.HashMap;
import java.util.HashSet;
import com.smart.vo.AppUserVo;
import com.smart.util.Messages;
import com.smart.util.SysUserUtils;
import com.smart.entity.SysUser;
import com.smart.req.LoginParam;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import io.swagger.annotations.Api;

import com.smart.service.UserService;
import com.smart.service.CaptchaService;
import com.smart.service.OnlineUserService;
import com.smart.common.annotation.SysLog;
import com.smart.common.enums.BusinessType;
import com.smart.common.utils.JwtTokenUtil;
import io.swagger.annotations.ApiOperation;
import com.smart.common.utils.SecurityUtils;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author lgb
 * @date 2020年6月9日 上午10:19:53 Copyright (C) 深圳工勘科技
 * @说明: 系统登录入口 包括web前端,admin后台,app端登录
 **/
@Slf4j
@RestController("sysLoginController")
@Api(tags="登陆管理")
public class SysLoginController extends BaseController {

	@Resource
	private JwtTokenUtil jwtTokenUtil;
	@Resource
	private UserService userService;
	@Resource
	private CaptchaService captchaService;
	@Resource
    private OnlineUserService onlineUserService;
	/**
	 * @param request
	 * @param param username:电话号码 password:密码
	 * @return app登录
	 **/
	@ApiOperation(value = "app登录")
	@PostMapping(value = "/login/app")
	public Messages<AppUserVo> appLogin(HttpServletRequest request,
			@RequestBody LoginParam param) {
		log.info("======="+param.toString());
		AppUserVo vo = userService.appLogin(param.getUsername(),
				param.getPassword(),request);
		return Messages.success(vo);
	}

	@ApiOperation(value = "后台登录")
	@PostMapping(value = "/admin/login")
	@SysLog(title="部门管理",businessType=BusinessType.INSERT)
	public Messages<Map<String,String>> login(HttpServletRequest req, 
			@RequestBody LoginParam loginParam) {
		log.info("======="+loginParam.toString());
		boolean captcha=captchaService.validate(loginParam.getUuid(),loginParam.getCode());
		if (!captcha) {
			return Messages.validateFailed("验证码不正确");
		}
		AppUserVo userVo=userService.login(loginParam.getUsername(),loginParam.getPassword(),req);
		if (userVo == null) {
			return Messages.validateFailed("用户名或密码错误");
		}
		Map<String,String> tokenMap = new HashMap<>();
		tokenMap.put("token", userVo.getToken());
		tokenMap.put("tokenHead", jwtTokenUtil.getTokenHead());
		tokenMap.put("expiration", jwtTokenUtil.getExpiration());
		return Messages.success(tokenMap);
	}

	@ApiOperation(value="info => 获取当前登录用户信息")
	@GetMapping(value="/admin/info")
	public Messages<Map<String, Object>> getInfo() {
		SysUser user = SysUserUtils.getSysUser();
		Map<String, Object> data = new HashMap<>();
		Set<String> perms = new HashSet<>();
		Set<String> roles = new HashSet<>();
		perms.add("*:*:*");
		// 管理员拥有所有权限
		if (SecurityUtils.isAdmin(user.getId())) {
			roles.add("admin");
		} else {
			roles.add("other");
		}
		data.put("name", user.getUsername());
		data.put("roles", roles);
		data.put("permissions", perms);
		data.put("avatar", user.getIcon());
		return Messages.success(data);
	}

	@ApiOperation(value="logout => 登出功能")
    @PostMapping(value="/admin/logout")
    public Messages<Object> logout(HttpServletRequest request) throws Exception {
        onlineUserService.logout(jwtTokenUtil.getToken(request));
        SecurityContextHolder.clearContext();
        return Messages.success(null);
    }
	
	@ApiOperation(value="app => 获取版本号")
    @GetMapping(value="/app/version")
    public Messages<Object> version() {
		Map<String,Object> data = new HashMap<>();
        data.put("versionName","1.0");
        data.put("content","");
        data.put("size","");
        data.put("varsionCode","1");
        data.put("url","http://192.168.10.124:8080/download/test1.apk");
        data.put("boolean", false);
        return Messages.success(data);
    } 
	
	
}